package com.tianheng.modbus.enums;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.tianheng.modbus.exception.EnumParseException;
import lombok.SneakyThrows;

import java.util.HashMap;
import java.util.Map;

public enum FunctionCodeType {
    C01("01", "读取线圈状态（01）"),
    C02("02", "读取输入状态（02）"),
    C03("03", "读取保存寄存器（03）"),
    C04("04", "读取输入寄存器（04）"),
    C05("05", "强置单线圈（05）"),
    C06("06", "预置单寄存器（06）");

    private String code;
    private	 String name;

    FunctionCodeType(String code, String name){
        this.code = code;
        this.name = name;
    }
    public String getCode(){
        return this.code;
    }
    public String getName(){
        return this.name;
    }
    @JsonCreator
    @SneakyThrows
    public static FunctionCodeType getByCode(String code){
        for(FunctionCodeType functionCodeType: FunctionCodeType.values()){
            if(functionCodeType.getCode().equals(code)){
                return functionCodeType;
            }
        }
        throw new EnumParseException(FunctionCodeType.class, code);
    }
    @JsonValue
    public Map<String, String> toJson(){
        Map map = new HashMap<String, String>();
        map.put("code", getCode());
        map.put("name", getName());
        return map;
    }
}
